package com.laikan.reader.core.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.laikan.reader.core.LaikanBaseAdapter;
import com.laikan.reader.R;
import com.laikan.reader.core.bean.ReadVolumesBean;
import com.laikan.reader.core.utils.StringUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import static com.laikan.reader.R.id.tv_balance;

/**
 * @author 阅读劵信息
 */
public class PersonalYdzAdapter extends LaikanBaseAdapter<ReadVolumesBean> {

    public PersonalYdzAdapter(Context context, ArrayList<ReadVolumesBean> mData) {
        super(context, mData);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            // 获取控件
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.personal_ydz_item02, null);
            holder.tv_balance = (TextView)convertView.findViewById(tv_balance);
            holder.tv_total = (TextView)convertView.findViewById(R.id.tv_total);
            holder.tv_activeTime = (TextView)convertView.findViewById(R.id.tv_activeTime);
            holder.tv_expireTime = (TextView)convertView.findViewById(R.id.tv_expireTime);
            convertView.setTag(holder);
        }else {
            // 复用控件
            holder = (ViewHolder)convertView.getTag();
        }

        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            // String activeTime = df.format(new Date(new Long(datas.get(position).getActiveTime())));

            Long expireTimeL = new Long(datas.get(position).getExpireTime());
            Long diffTime = expireTimeL - System.currentTimeMillis();
            String diffTimeStr = StringUtil.diffTime2(diffTime);

            // 设置控件的值

            holder.tv_balance.setText(datas.get(position).getTotal());
            holder.tv_balance.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
            holder.tv_balance.getPaint().setFakeBoldText(true);//加粗
            holder.tv_total.setText("余额: " + datas.get(position).getBalance());
            holder.tv_activeTime.setText("过期: " + df.format(new Date(new Long(expireTimeL))));
            holder.tv_expireTime.setText(diffTimeStr);

        }catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    private class ViewHolder {

        private TextView tv_balance;
        private TextView tv_total;
        private TextView tv_activeTime;
        private TextView tv_expireTime;
    }

}
